﻿using System;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Uow;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.MySQL;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.Modularity;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement.EntityFrameworkCore;
using Ebusiness_EntityFrameWork.DBContext;
using Microsoft.EntityFrameworkCore;
using AbpVnext_Module;
using AbpVnext_Module.DBContext;
using Ebusiness_Domain.DomainService;
using Volo.Abp.Auditing;

namespace YDT_ABPvNext_Projects.EntityFrameworkCore;

[DependsOn(
    typeof(YDT_ABPvNext_ProjectsDomainModule),
    typeof(AbpIdentityEntityFrameworkCoreModule),
    typeof(AbpPermissionManagementEntityFrameworkCoreModule),
    typeof(AbpEntityFrameworkCoreMySQLModule),
    typeof(AbpAuditLoggingEntityFrameworkCoreModule),
    typeof(AbpTenantManagementEntityFrameworkCoreModule)
    )]
public class YDT_ABPvNext_ProjectsEntityFrameworkCoreModule : AbpModule
{
    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        YDT_ABPvNext_ProjectsEfCoreEntityExtensionMappings.Configure();
    }

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddAbpDbContext<YDT_ABPvNext_ProjectsDbContext>(options =>
        {
                /* Remove "includeAllEntities: true" to create
                 * default repositories only for aggregate roots */
            options.AddDefaultRepositories(includeAllEntities: true);
        });

        //Configure<AbpDbContextOptions>(options =>
        //{
        //        /* The main point to change your DBMS.
        //         * See also YDT_ABPvNext_ProjectsMigrationsDbContextFactory for EF Core tooling. */
        //    options.UseMySQL();
        //});

        //注册默认全局上下文
        //dotnet ef migrations add project1  -c EbusinessDbContext   --project  "D:\学习\项目\Abp_DDD\abp\webApi\Ebusiness_EntityFrameWork\Ebusiness_EntityFrameWork.csproj"
        context.Services.AddDbContext<EbusinessDbContext>(options =>
        {
            // 2、配置MySQL
            options.UseMySql("server=192.168.211.129;port=3306;database=AspNetCore_ProductImage;uid=root;pwd=123456",
                 //指定程序集目录
                 new MySqlServerVersion(new Version(5, 7, 29)));
        });

        //生成迁移文件
        //dotnet ef  migrations add project1   -c  ProjectDbContext   --project  "D:\学习\项目\Abp_DDD\abp\webApi\Ebusiness_EntityFrameWork\Ebusiness_EntityFrameWork.csproj"
        //dotnet ef  database update   -c  ProjectDbContext   --project  "D:\学习\项目\ABP\abp\webApi\Ebusiness_EntityFrameWork\Ebusiness_EntityFrameWork.csproj"
        //注册ABP全局上下文 ProjectDbContext
        context.Services.AddAbpDbContext<ProjectDbContext>();

        //dotnet ef  migrations add project1   -c  ProductImageDbContext   --project  "D:\学习\项目\Abp_DDD\abp\webApi\Ebusiness_EntityFrameWork\Ebusiness_EntityFrameWork.csproj"
        //注册商品图片上下文 注册ABP全局上下文
        context.Services.AddAbpDbContext<ProductImageDbContext>();

        //context.Services.AddTransient<IProductRepository, ProductRepository>();

        ///注册身份认证的服务依赖  
        ///泛型俩个参数分别是用户数据模型和角色模型
        ///AddEntityFrameworkStores 通过这个函数连接数据库上下文对象   泛型为对应的上下文对象。
        //微软Identity数据库上下文
        //context.Services.AddDbContext<AppDbContext>(options =>
        //       options.UseMySql("server=127.0.0.1;port=3306;database=AspNetCore_ProductImage_Identity;uid=root;pwd=123456",
        //            //指定程序集目录
        //            new MySqlServerVersion(new Version(5, 7, 29)))
        //   );
        //集成mysql   
        Configure<AbpDbContextOptions>(options =>
        {
            options.UseMySQL();
            //var config = configuration.GetSection("DatabaseType").Value;
            //Console.WriteLine(config);
            ////动态切换数据
            //if (config.Equals("SqlServer"))
            //{
            //    option.UseSqlServer();
            //}
            //else
            //{
            //    option.UseMySQL();
            //}
        });

        #region 应用审计模块原理
        {
            //审计日志执行的原理
            //AuditingInterceptorRegistrar
            //----> AuditingInterceptor
            //----->IAuditingManager--->AuditingManager（管理IAuditLogSaveHandle ）
            //----->IAuditLogSaveHandle(保存升级日志)
            //--->AuditingManager
            //--->IAuditingStore--->(审计日志存储)AuditingStore
            //Volo.Abp.Auditing
            //--->IAuditLogRepository-- > EfCoreAuditLogRepository(日志记录到数据库中)
            //Volo.Abp.AuditLogging.Domain Volo.Abp.AuditLogging.EntityFrameworkCore
        }
        #endregion
        Configure<AbpAuditingOptions>(options =>
        {
            options.IsEnabled = true;  //启动审计
            options.IsEnabledForGetRequests = true;  //启用记录get请求
        });
    }
}
